﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class public_page_user_infoMod : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(public_page_user_infoMod));
        if (!this.IsPostBack)
        {
            SysCommon.checkUserLogin();

            this.BtnOK.Attributes.Add("onclick", "return check_submit()");
            this.Txt_email.Attributes.Add("onchange", "javascript:onchanged_email()");


            DataTable doc_type_dt = DataBaseOperate_SQLServer.GetDataTable("select doc_type_id,doc_name from t_doc_type");

            this.Ddl_doc_type_id.DataSource = doc_type_dt;
            this.Ddl_doc_type_id.DataValueField = "doc_type_id";
            this.Ddl_doc_type_id.DataTextField = "doc_name";
            this.Ddl_doc_type_id.DataBind();


            DataRow dr = DataBaseOperate_SQLServer.GetDataRow("select * from t_user where user_id=" + SysCommon.ProcessSqlStr(Session["user_id"]));

            if (dr != null)
            {
                ContentPlaceHolder cph_Right;
                cph_Right = (ContentPlaceHolder)Master.FindControl("CPH_Right");

                String[] arrField = { "user_name", "gender", "email", "mobile", "true_name", "doc_type_id", "doc_num", "telephone", "address", "zip_code" };
                for (int i = 0; i < arrField.Length; i++)
                {

                    if ((cph_Right.FindControl("Txt_" + arrField[i]) is TextBox))
                    {
                        ((TextBox)(cph_Right.FindControl("Txt_" + arrField[i]))).Text = Convert.ToString(dr[arrField[i]]);
                    }
                    else if ((cph_Right.FindControl("Lbl_" + arrField[i]) is Label))
                    {
                        ((Label)(cph_Right.FindControl("Lbl_" + arrField[i]))).Text = Convert.ToString(dr[arrField[i]]);
                    }
                    else if ((cph_Right.FindControl("Ddl_" + arrField[i]) is DropDownList))
                    {
                        ((DropDownList)(cph_Right.FindControl("Ddl_" + arrField[i]))).SelectedValue = Convert.ToString(dr[arrField[i]]);
                    }
                    else if ((cph_Right.FindControl("Chk_" + arrField[i]) is CheckBox))
                    {
                        if(Convert.ToInt32(dr[arrField[i]]) ==1)
                        {
                            ((CheckBox)(cph_Right.FindControl("Chk_" + arrField[i]))).Checked = true;
                        }
                        else{
                            ((CheckBox)(cph_Right.FindControl("Chk_" + arrField[i]))).Checked = false;
                        }
                    }
                }
            }
        }


    }

    protected void BtnOK_Click(object sender, EventArgs e)
    {
        bool sql_success = false;

        SqlTransaction myTrans = DataBaseOperate_SQLServer.objConnection.BeginTransaction();

        try
        {
            SqlCommand objCommand = new SqlCommand();
            objCommand.Connection = DataBaseOperate_SQLServer.objConnection;
            objCommand.Transaction = myTrans;

            objCommand.CommandText = "select count(1) from t_user where user_id<>" + SysCommon.ProcessSqlStr(Session["user_id"]) + " and email='" + SysCommon.ProcessSqlStr(this.Txt_email.Text) + "'";

            if ((int)objCommand.ExecuteScalar() > 0)
            {
                SysCommon.JavaScript.alert("该邮箱已注册，请重新输入邮箱地址！");

                myTrans.Rollback();

                return;
            }

            String[] arrField = { "gender", "email", "mobile", "true_name", "doc_type_id", "doc_num", "telephone", "address", "zip_code" };

            System.Text.StringBuilder sbstr = new System.Text.StringBuilder(200);
            sbstr.Append("update t_user set ");
            for (int i = 0; i < arrField.Length; i++)
            {
                sbstr.Append(arrField[i]);
                sbstr.Append("=@");
                sbstr.Append(arrField[i]);
                if (i < arrField.Length - 1)
                {
                    sbstr.Append(",");
                }
            }
            sbstr.Append(" where user_id=");
            sbstr.Append(SysCommon.ProcessSqlStr(Session["user_id"]));
            objCommand.CommandText = sbstr.ToString();

            SqlParameter tempParameter;


            ContentPlaceHolder cph_Right;
            cph_Right = (ContentPlaceHolder)Master.FindControl("CPH_Right");

            for (int i = 0; i < arrField.Length; i++)
            {

                if ((cph_Right.FindControl("Txt_" + arrField[i]) is TextBox))
                {
                    tempParameter = new SqlParameter("@" + arrField[i], SqlDbType.VarChar);
                    tempParameter.Value = ((TextBox)(cph_Right.FindControl("Txt_" + arrField[i]))).Text;
                    objCommand.Parameters.Add(tempParameter);
                }
                else if ((cph_Right.FindControl("Ddl_" + arrField[i]) is DropDownList))
                {
                    objCommand.Parameters.Add("@" + arrField[i], ((DropDownList)(cph_Right.FindControl("Ddl_" + arrField[i]))).SelectedValue);
                }
                else if ((cph_Right.FindControl("Chk_" + arrField[i]) is CheckBox))
                {
                    objCommand.Parameters.Add("@" + arrField[i], ((CheckBox)(cph_Right.FindControl("Chk_" + arrField[i]))).Checked == true ? 1 : 0);
                }
                else
                {
                    objCommand.Parameters.Add("@" + arrField[i], "");
                }
            }
            //DataBaseOperate_SQLServer.ExecUpdate(objCommand);
            objCommand.ExecuteNonQuery();

            
            myTrans.Commit();

            sql_success = true;

        }
        catch (Exception ex)
        {
            Log.Record(ex);
            myTrans.Rollback();
        }

        if (sql_success == true)
        {
            Response.Redirect("user_toIndex.aspx");
        }


    }

    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
    public bool check_user_name(string temp_user_name, int name_or_email)
    {
        int find_user = -1;
        if (name_or_email == 1)
        {
            find_user = (int)DataBaseOperate_SQLServer.GetValue("select count(1) from t_user where email='" + SysCommon.ProcessSqlStr(temp_user_name) + "'");
        }

        if (find_user == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
